<?php
mysql_query("delete from user_temp_result where u_id = ".$_SESSION['user_id']);
mysql_query("insert into user_temp_result(u_id,start_date) values('".$_SESSION['user_id']."',now())");
require("test_functions.php");

//---------------------GENERAL VARIABLES------------------------------------------------------
$arr_com = array();//array of complexity
$arr_count = array();//array of amount of questions
$arr_except = array();//array of ids of questions,which has less than necessary
$questions = array();//array of complete question packet
$com = 1;//complexity.

//---------------------DEFINE AMOUNT OF COMPLEXITY--------------------------------------------
$com_res = mysql_query("select distinct q_complexity from questions where cat_id = ".$_SESSION['category_id']." order by q_complexity");
$com_arr = mysql_fetch_array($com_res);
$arr_com[] = $com_arr[0];

while($a_res = mysql_fetch_array($com_res)){
	if(!checkRepetition($a_res[0],$arr_com)){
		$arr_com[] = $a_res[0];
	}
}

$com = sizeof($arr_com);//complexity
$count = round(25/$com);//amount of questions of each complexity
$rem = 25%$com;//reminder

//---------------------CHECKING AMOUNT OF QUESTIONS OF EACH COMPLEXITY------------------------
$null_com = 0;//number of not used complexity
$except_com = 0;//number of used complexity with specified amount of questions
$except_count = 0;//amount of all except questions
for($i=0; $i<$com; $i++){
	$temp_count = $count;
	$checking_res = mysql_query("select count(q_id) as amount_of_questions from questions where q_complexity = ".$arr_com[$i]." and cat_id = ".$_SESSION['category_id']);
	$checking_a = mysql_fetch_array($checking_res);
	$ques_count = $checking_a[0];
	if($ques_count <= $count+2){
		$rand_choose = rand(0,1);
		if($rand_choose == 0){
			$arr_com[$i] = 'null';
			$null_com++;
		}
		else{
			$temp_count = rand(1,$ques_count);
			$except_count += $temp_count;
			$arr_except[$i] = 'except';
			$except_com++;
		}
	}
	$arr_count[$i] = $temp_count;
}

//---------------------DEFINE NUMBERS OF QUESTIONS OF EACH COMPLEXITY-------------------------
$buf_com = $com-$null_com-$except_com;//numbers of valid complexities
$new_count = floor((25-$except_count)/($com-$null_com-$except_com));//new amount of questions of each complexity
$max_ques = $buf_com*$new_count;
$rem = 25-$max_ques-$except_count;//not used questions(reminder)
$arr_count = fillArray($arr_except,$arr_count,$new_count);
//---------------------STARTING GENERATING QUIZ-----------------------------------------------
$q_count = 1;//questions count
$ques_array = array();//array of questions
$titles_array = array();//array of titles of questions
$ques_array = quesToArray(mysql_query("select * from choices order by c_order"));
$titles_array = titlesToArray(mysql_query("select * from questions order by q_id"));

for($i=0; $i<$com; $i++){
	if($arr_com[$i] != 'null'){
		$array_id = getIds($arr_com[$i]);
		$max_rand = sizeof($array_id)-1;
		for($j=1; $j<=$arr_count[$i]; $j++){
			$rem_count = 0;
			$rem_ceiling = 1;
			if($j == 1 && $rem > 0){
				$rem_ceiling++;
				$rem--;
			}
			while($rem_count < $rem_ceiling){
	//---------------------TAKING CHOICES---------------------------------------------------------
				$select = rand(0,$max_rand);//selects random index in question ids' array
				mysql_query("update user_temp_result set q".$q_count."_id = '".$array_id[$select]."' where u_id='".$_SESSION['user_id']."'");
				$questions[] = getFullQuestion($array_id[$select],$q_count,$ques_array,$titles_array);
				$q_count++;
	//-----------------------------------------------------------------------------------------------
				$array_id = removeID($array_id,$select);
				$max_rand--;
				$rem_count++;
			}
		}
	}
}

header("Location:?option=test&task=load");
$smarty -> assign('component', 'test');
$smarty -> assign('title','Test');
$smarty -> assign('questions', $questions);
$smarty -> display('test.tpl');
?>
